home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / Collection.st < prev    next >
Text File  |  2000-02-13  |  2KB  |  99 lines

  1. Class Collection :Object
  2. [
  3.    addAll: aCollection
  4.      aCollection do: [:x | self add: x ]
  5. |
  6.    asArray
  7.       ^ Array new: self size ;
  8.         replaceFrom: 1 to: self size with: self
  9. |
  10.    asBag
  11.       ^ Bag new addAll: self
  12. |
  13.    asSet
  14.       ^ Set new addAll: self
  15. |
  16.    asList
  17.       ^ List new addAllLast: self
  18. |
  19.    asString
  20.       ^ String new: self size ; 
  21.          replaceFrom: 1 to: self size with: self
  22. |
  23.    coerce: aCollection   ! newobj !
  24.       newobj <- self new.
  25.       aCollection do: [:x | newobj add: x].
  26.       ^ newobj
  27. |
  28.    collect: aBlock
  29.       ^ self inject: self class new
  30.              into: [:x :y | x add: (aBlock value: y). x ]
  31. |
  32.    deepCopy     ! newobj !
  33.       newobj <- List new .
  34.       self do: [:x | newobj addLast: x copy ].
  35.       ^ self coerce: newobj
  36. |
  37.    detect: aBlock
  38.       ^ self detect: aBlock
  39.       ifAbsent: [self error: 'no object found matching detect']
  40. |
  41.    detect: aBlock ifAbsent: exceptionBlock   
  42.       self do: [:x | (aBlock value: x) ifTrue: [^ x]].
  43.       ^ exceptionBlock value
  44. |
  45.    first
  46.       ^ self error: 'subclass should implement first'
  47. |
  48.    includes: anObject
  49.       self do: [:x | (x = anObject) ifTrue: [^ true]].
  50.       ^ false
  51. |
  52.    inject: thisValue into: binaryBlock     ! last !
  53.       last <- thisValue.
  54.       self do: [:x | last <- binaryBlock value: last value: x].
  55.       ^ last
  56. |
  57.    isEmpty
  58.       ^ (self size = 0)
  59. |
  60.    occurrencesOf: anObject
  61.       ^ self inject: 0
  62.                into: [:x :y | (y = anObject) 
  63.                       ifTrue: [x + 1]
  64.                       ifFalse: [x] ]
  65. |
  66.    printString
  67.       ^ ( self inject: self class printString , ' ('
  68.           into: [:x :y | x , ' ' , y printString]), ' )'
  69. |
  70.    reject: aBlock          
  71.       ^ self select: [:x | (aBlock value: x) not ]
  72. |
  73.    remove: oldObject
  74.       self remove: oldObject ifAbsent:
  75.                   [^ self error: 
  76.                      'attempt to remove object not found in collection' ].
  77.       ^ oldObject
  78. |
  79.    remove: oldObject ifAbsent: exceptionBlock
  80.       ^ (self includes: oldObject)
  81.          ifTrue: [self remove: oldObject]
  82.          ifFalse: exceptionBlock
  83. |
  84.    select: aBlock          
  85.       ^ self inject: self class new
  86.              into: [:x :y | (aBlock value: y) 
  87.                     ifTrue: [x add: y]. x]
  88. |
  89.    shallowCopy  ! newobj !
  90.       newobj <- List new .
  91.       self do: [:x | newobj addLast: x].
  92.       ^ self coerce: newobj
  93. |
  94.    size ! i !
  95.       i <- 0.
  96.       self do: [:x | i <- i + 1 ].
  97.       ^ i
  98. ]
  99.